package com.liyuhan.onlinetaxibackground.Service.impl;

import com.google.gson.Gson;
import com.liyuhan.onlinetaxibackground.POJO.Comment;
import com.liyuhan.onlinetaxibackground.POJO.CompletedOrder;
import com.liyuhan.onlinetaxibackground.Repository.CommentRepository;
import com.liyuhan.onlinetaxibackground.Repository.CompletedOrderRepository;
import com.liyuhan.onlinetaxibackground.Service.CommentService;
import com.liyuhan.onlinetaxibackground.Util.GetRequestBody;
import com.liyuhan.onlinetaxibackground.dto.CommentDto.CommentDto;
import com.liyuhan.onlinetaxibackground.dto.DataRequest;
import com.liyuhan.onlinetaxibackground.dto.DataResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private CompletedOrderRepository completedOrderRepository;

    // 响应参数
    private Map<String, String> map = new HashMap<>();

    @Override
    public DataResponse doComment(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        // 获取用户类型
        String userType = requestBody.getData().get("userType").toString();
        // 获取订单号
        String orderId = requestBody.getData().get("orderId").toString();
        // 获取星级
        String score = requestBody.getData().get("score").toString();
        // 获取内容
        String content = requestBody.getData().get("content").toString();

        String state;
        if (userType.equals("driver")) {
            state = "1";
        } else {
            state = "0";
        }

        // 根据订单获取completedOrder
        CompletedOrder order = completedOrderRepository.findById(Integer.valueOf(orderId)).get();

        // 创建评论对象
        Comment comment = new Comment(
                new Date(),
                Double.parseDouble(score),
                content,
                state,
                order,
                order.getPassenger(),
                order.getDriver()
        );

        // 保存评论对象
        commentRepository.save(comment);

        return new DataResponse("200", null);
    }

    @Override
    public DataResponse getComments(String request) {

        DataRequest requestBody = GetRequestBody.getRequestBody(request);
        map.clear();

        String orderId = requestBody.getData().get("orderId").toString();

        // 获取全部评论
        List<Comment> comments = commentRepository.findCommentsByOrder(completedOrderRepository.findById(Integer.valueOf(orderId)).get()).get();

        Gson gson = new Gson();

        CommentDto commentDto;
        if (comments.size() > 0) {
            for (Comment comment : comments) {
                if (comment.getState().equals("0")) {
                    // 乘客
                    commentDto = new CommentDto(
                            "passenger",
                            comment.getCommentTime(),
                            comment.getScore().toString(),
                            comment.getContent()
                    );
                    map.put("passenger", gson.toJson(commentDto));
                } else if (comment.getState().equals("1")) {
                    // 乘客
                    commentDto = new CommentDto(
                            "driver",
                            comment.getCommentTime(),
                            comment.getScore().toString(),
                            comment.getContent()
                    );
                    map.put("driver", gson.toJson(commentDto));
                }
            }
        }

        return new DataResponse("200", map);
    }
}
